use std::sync::Arc;

use axum::{
    routing::{delete, get, patch, post, put},
    Router,
};
use tokio::sync::{mpsc, oneshot};

use crate::app_state::state::State;
use crate::types::channel::{Request, Response};

use super::*;

// v1 routes
pub fn v1_new(
    tx_to_rauth_grpc_channel: mpsc::Sender<(Request, oneshot::Sender<Response>)>,
    tx_to_iblog_grpc_channel: mpsc::Sender<(Request, oneshot::Sender<Response>)>,
    tx_to_redis_channel: mpsc::Sender<(Request, oneshot::Sender<Response>)>,
) -> Router {
    Router::new()
        .route("/", get(home::home))
        // user routes
        .route("/register", post(user::register))
        .route("/login", post(user::login))
        .route("/logout/:id", get(user::logout))
        .route("/user/:id", get(user::get))
        .route("/user/:id", put(user::update))
        .route("/user/:id", delete(user::delete))
        .route("/user/:id/password", patch(user::change_password))
        // blog routes
        .route("/blog", post(blog::add))
        .route("/blog", get(blog::get))
        .route("/blogs", get(blog::get_multiple))
        .route("/blog/update", patch(blog::update))
        .route("/blog", delete(blog::delete))
        .route("/blog/view-inc", get(blog::view_inc))
        .route("/blog/star-inc", get(blog::star_inc))
        .route("/blog/star-dec", get(blog::star_dec))
        // idea routes
        .route("/idea", post(idea::add))
        .route("/idea", get(idea::get))
        .route("/ideas", get(idea::get_multiple))
        .route("/idea/update", patch(idea::update))
        .route("/idea", delete(idea::delete))
        .route("/idea/view-inc", get(idea::view_inc))
        .route("/idea/star-inc", get(idea::star_inc))
        .route("/idea/star-dec", get(idea::star_dec))
        // about routes
        .route("/about", get(about::about))
        .with_state(Arc::new(State::new(
            tx_to_rauth_grpc_channel,
            tx_to_iblog_grpc_channel,
            tx_to_redis_channel,
        )))
    // .layer(ServiceBuilder::new().layer(middleware::from_fn(logging_middleware)))
}

pub fn v1_healthy_new() -> Router {
    Router::new()
        .route("/alive", get(healthy::alive))
        .route("/ready", get(healthy::ready))
}

// if let Some(x_user_id) = (&_request.headers()).get("x-user-id") {
//     // let mut state = Extension(Arc::new(AppState::default()));
//     _state.user_id = x_user_id
//         .to_owned()
//         .to_str()
//         .unwrap()
//         .parse::<usize>()
//         .unwrap();
// }
